import React from 'react';
import {SafeAreaView, ToastAndroid, View} from "react-native";
import { StyleSheet } from 'react-native';
import {
    Button,
    Card,
    Divider,
    Icon, IndexPath,
    Input,
    Layout, List, Radio, RadioGroup, Select, SelectItem,
    Tab,
    TabView,
    Text,
    TopNavigation,
    TopNavigationAction
} from '@ui-kitten/components';
import {showMessage} from "react-native/Libraries/Utilities/LoadingView";



export const SelectFixItemScreen = ({ route,navigation }) => {

    const {result,id,rkid,wxr} = route.params;

    //选择维修方式组件
    const [selectFixData, setSelectFixData] = React.useState('');
    const [selectedFixIndex, setSelectedFixIndex] = React.useState(new IndexPath(0));
    const [displayFixValue, setDisplayFixValue] = React.useState("已处理");
    const fixList = ['已处理','小辅修处理','待料','临修（检修车间）'];

    const [value, setValue] = React.useState('');

    const [selectedIndex, setSelectedIndex] = React.useState(0);

    const showToast = (message) => {
        ToastAndroid.show(message, ToastAndroid.SHORT);
    };

    /**
     * 完成按钮
     * @param bz => 备注
     * @param wxwcfs
     * @param id
     */
    const acceptFix = (bz,wxwcfs,id,j28,rkid,wxr)=>{
        const requestOptions = {
            method: 'GET',
            redirect: 'follow'
        };

        if (j28==="请选择"){
           showToast("请先选择机统28")
        }else {
            const url = "http://htjoa.nrnet.cn/dc14/jwmis/acceptancerepair/repairdonebyid?bz="+bz+"&wxwcfs="+wxwcfs+"&j28="+j28+"&id="+id+"&rkid="+rkid+"&wxr="+wxr
            console.log(url)
            fetch(url, requestOptions)
                .then(response => response.text())
                .then(result => {
                    console.log(result,"完成按钮")
                    if (result === 'success'){
                        navigation.navigate("RepairComplete")
                    }else {
                       showToast("提交失败 请刷新页面")
                    }


                })
                .catch(error => console.log('error', error));
        }


        console.log(bz,wxwcfs,id,j28,"------------------")


    }


    const Header = (props) => (
        <View {...props}>
            <Text category='h5' >
                请选择维修方式:
            </Text>
        </View>
    );
    // const navigateAddTicketCollection = () => {
    //     navigation.navigate('AddTicketCollection');
    // };


    return (
        <SafeAreaView style={{ flex: 1 }}>
            <Card style={styles.card} header={Header} >
                <RadioGroup
                    selectedIndex={selectedIndex}
                    onChange={index => setSelectedIndex(index)}>
                    {result.map((item,index)=>{
                        return(
                            <Radio key={index}>
                                {item["operMethod"]}
                            </Radio>
                        )
                    })}
                </RadioGroup>

                <Select
                    style={{marginBottom:40,marginTop:30}}
                    placeholder='请选择维修项目'
                    value={displayFixValue}
                    selectedIndex={selectedFixIndex}
                    onSelect={index => {setSelectedFixIndex(index)
                        setDisplayFixValue(fixList[index["row"]])


                    }
                    }>
                    {
                        fixList.map((item,index)=>{
                            return(  <SelectItem key={index} title={item}/>)
                        })
                    }
                    {/*{firstData.map["name"](renderOption)}*/}
                </Select>


                <Input
                    placeholder='请输入备注'
                    value={value}
                    onChangeText={nextValue => setValue(nextValue)}
                />

                <Divider/>
                <Button
                    style={{marginTop:20}}

                    onPress={()=>{
                    acceptFix(value, result[selectedIndex]["id"],id,displayFixValue,rkid,wxr)
                }}>
                    提交
                </Button>

                <Button style={{marginTop:10}} onPress={() => {

                    navigation.navigate("RepairComplete")

                }}
                        appearance='outline'
                        status='warning'
                >
                    返回
                </Button>


            </Card>

            {/*<Button*/}
            {/*    onPress={navigateServicingTicket}*/}
            {/*    style={styles.footerControl}*/}
            {/*    appearance='outline'*/}
            {/*    status='warning'>*/}
            {/*    结束添加*/}
            {/*</Button>*/}

            {/*<Button*/}
            {/*    style={styles.footerControl}*/}
            {/*    onPress={()=>{*/}
            {/*        navigation.navigate('AddTicketCollection',{cx:cx,ch:ch,cxid:cxid,id:id})*/}
            {/*    }*/}
            {/*    }*/}
            {/*>*/}
            {/*    继续添加*/}
            {/*</Button>*/}


        </SafeAreaView>
    );
};



const styles = StyleSheet.create({
    topContainer: {
        flexDirection: 'row',
        justifyContent: 'space-between',
    },
    card: {
        flex: 1,
        margin: 2,
    },
    footerContainer: {
        flexDirection: 'row',
        justifyContent: 'flex-end',
    },
    footerControl: {
        marginHorizontal: 2,
    },
});